import { readdir, rename } from 'node:fs/promises'
import { join, extname, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'

const __dirname = dirname(fileURLToPath(import.meta.url))

const codes = [
  0x4e00, 0x4e01, 0x4e03, 0x4e07, 0x4e08, 0x4e09, 0x4e09, 0x4e0a, 0x4e0b, 0x4e0d, 0x4e0e, 0x4e10,
  0x4e11, 0x4e13, 0x4e14, 0x4e16, 0x4e18, 0x4e19, 0x4e1a, 0x4e1b, 0x4e1c, 0x4e1d, 0x4e22, 0x4e24,
  0x4e25, 0x4e27, 0x4e2a, 0x4e2d, 0x4e30, 0x4e32, 0x4e34, 0x4e38, 0x4e39, 0x4e3a, 0x4e3b, 0x4e3d,
  0x4e3e, 0x4e43, 0x4e45, 0x4e48, 0x4e49, 0x4e4b, 0x4e4c, 0x4e4d, 0x4e4e, 0x4e4f, 0x4e50, 0x4e52,
  0x4e53, 0x4e54, 0x4e56, 0x4e58, 0x4e59, 0x4e5d, 0x4e5e, 0x4e5f, 0x4e60, 0x4e61, 0x4e66, 0x4e70,
  0x4e71, 0x4e73, 0x4e86, 0x4e88, 0x4e89, 0x4e8b, 0x4e8c, 0x4e8e, 0x4e8f, 0x4e91, 0x4e92, 0x4e94,
  0x4e95, 0x4e9a, 0x4e9b, 0x4ea1, 0x4ea4, 0x4ea5, 0x4ea6, 0x4ea7, 0x4ea9, 0x4eab, 0x4eac, 0x4ead,
  0x4eae, 0x4eb2, 0x4eba, 0x4ebf, 0x4ec0, 0x4ec1, 0x4ec5, 0x4ec6, 0x4ec7, 0x4eca, 0x4ecb, 0x4ecd,
  0x4ece, 0x4ed1, 0x4ed3, 0x4ed4, 0x4ed6, 0x4ed7, 0x4ed8, 0x4ed9, 0x4ee3, 0x4ee4, 0x4ee5, 0x4eea,
  0x4eec, 0x4ef0, 0x4ef2, 0x4ef6, 0x4ef7, 0x4efb, 0x4efd, 0x4eff, 0x4f01, 0x4f0a, 0x4f0d, 0x4f0f,
  0x4f10, 0x4f11, 0x4f17, 0x4f18, 0x4f19, 0x4f1a, 0x4f1e, 0x4f1f, 0x4f20, 0x4f24, 0x4f26, 0x4f2a,
  0x4f2f, 0x4f30, 0x4f34, 0x4f36, 0x4f38, 0x4f3a, 0x4f3c, 0x4f43, 0x4f46, 0x4f4d, 0x4f4e, 0x4f4f,
  0x4f51, 0x4f53, 0x4f55, 0x4f59, 0x4f5b, 0x4f5c, 0x4f60, 0x4f63, 0x4f69, 0x4f73, 0x4f7f, 0x4f84,
  0x4f88, 0x4f8b, 0x4f8d, 0x4f9b, 0x4f9d, 0x4fa0, 0x4fa3, 0x4fa5, 0x4fa6, 0x4fa7, 0x4fa8, 0x4fae,
  0x4faf, 0x4fb5, 0x4fbf, 0x4fc3, 0x4fc4, 0x4fca, 0x4fcf, 0x4fd0, 0x4fd7, 0x4fd8, 0x4fdd, 0x4fe1,
  0x4fe9, 0x4fed, 0x4fee, 0x4fef, 0x4ff1, 0x4ffa, 0x500d, 0x5012, 0x5014, 0x5018, 0x5019, 0x501a,
  0x501f, 0x5021, 0x5026, 0x503a, 0x503c, 0x503e, 0x5047, 0x504e, 0x504f, 0x505a, 0x505c, 0x5065,
  0x5076, 0x5077, 0x507f, 0x5080, 0x5085, 0x508d, 0x50a8, 0x50ac, 0x50b2, 0x50bb, 0x50cf, 0x50da,
  0x50e7, 0x50f5, 0x50fb, 0x5112, 0x5121, 0x513f, 0x5141, 0x5143, 0x5144, 0x5145, 0x5146, 0x5148,
  0x5149, 0x514b, 0x514d, 0x5151, 0x5154, 0x515a, 0x515c, 0x5162, 0x5165, 0x5168, 0x516b, 0x516c,
  0x516d, 0x5170, 0x5171, 0x5173, 0x5174, 0x5175, 0x5176, 0x5177, 0x5178, 0x517b, 0x517c, 0x517d,
  0x5180, 0x5185, 0x5188, 0x518c, 0x518d, 0x5192, 0x5195, 0x5197, 0x5199, 0x519b, 0x519c, 0x51a0,
  0x51a4, 0x51ac, 0x51af, 0x51b0, 0x51b2, 0x51b3, 0x51b5, 0x51b6, 0x51b7, 0x51bb, 0x51c0, 0x51c4,
  0x51c6, 0x51c9, 0x51cc, 0x51cf, 0x51d1, 0x51db, 0x51dd, 0x51e0, 0x51e1, 0x51e4, 0x51eb, 0x51ed,
  0x51ef, 0x51f0, 0x51f3, 0x51f6, 0x51f8, 0x51f9, 0x51fa, 0x51fb, 0x51fd, 0x51ff, 0x5200, 0x5201,
  0x5203, 0x5206, 0x5207, 0x520a, 0x5211, 0x5212, 0x5217, 0x5218, 0x5219, 0x521a, 0x521b, 0x521d,
  0x5220, 0x5224, 0x5228, 0x5229, 0x522b, 0x522e, 0x5230, 0x5236, 0x5237, 0x5238, 0x5239, 0x523a,
  0x523b, 0x523d, 0x5242, 0x5243, 0x524a, 0x524d, 0x5251, 0x5254, 0x5256, 0x5265, 0x5267, 0x5269,
  0x526a, 0x526f, 0x5272, 0x527f, 0x5288, 0x529b, 0x529d, 0x529e, 0x529f, 0x52a0, 0x52a1, 0x52a3,
  0x52a8, 0x52a9, 0x52aa, 0x52ab, 0x52b1, 0x52b2, 0x52b3, 0x52bf, 0x52c3, 0x52c7, 0x52c9, 0x52cb
]

const names = []

function getRandom(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min
}

function getRandomChineseCharacter() {
  return String.fromCharCode(codes[getRandom(0, codes.length)]).replace('\x00', '')
}
function genName() {
  let name
  do {
    name = Array.from({ length: getRandom(5, 15) }, getRandomChineseCharacter).join('')
  } while (names.includes(name))

  names.push(name)

  return name
}

function anonymizeName(name) {
  const datePattern = /(\d{4}-\d{2}-\d{2})|(\d{8})|(\d{6})|(\d{4}-\d{2})/

  const extension = extname(name)

  const match = name.match(datePattern)

  let result = ''

  if (match) {
    result = '-' + match[0]
  }

  return `${genName()}${result}${extension}`
}

async function traverseDirectory(dirPath, depth = 0) {
  if (depth > 2) {
    return
  }

  const entries = await readdir(dirPath, { withFileTypes: true })

  for (let index = 0; index < entries.length; index++) {
    const entry = entries[index]
    const entryPath = join(dirPath, entry.name)

    const newName = anonymizeName(entry.name)
    const newPath = join(dirPath, newName)

    try {
      if (entry.isDirectory()) {
        if (entry.name === '.存档') {
          traverseDirectory(join(dirPath, '.存档'), depth)
        } else {
          await rename(entryPath, newPath)

          await traverseDirectory(newPath, depth + 1)
        }
      } else if (entry.isFile()) {
        await rename(entryPath, newPath)
      }
    } catch (error) {
      console.error(error)
      console.log({ name: entry.name, newName })
    }
  }
}

traverseDirectory(join(__dirname, './.todo'))
